package lucky.xiong.basic.编码;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64test {
    public static void main(String[] args) {
        String base64 = "DAJDQQ==";
        byte[] decode1 = Base64.getDecoder().decode(base64);
        System.out.println(new String(decode1));




        String inputString = "1234567812345678"; // 要验证的字符串

        boolean isValidBase64 = isBase64(inputString);
        System.out.println("Is valid Base64: " + isValidBase64);

        String str = "dafa612351S";
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] decode = Base64.getEncoder().encode(str.getBytes());
        String s = encoder.encodeToString(str.getBytes());
        System.out.println("s:"+s);
        System.out.println("byte[]:"+new String(decode));

    }

    public static boolean isBase64(String input) {
        try {
//            if (input == null || input.isEmpty()) {
//                return false;
//            }
//            String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
//            boolean isLegal = input.matches(base64Pattern);
//            if (!isLegal) {
//               return false;
//            }

            byte[] decode = Base64.getDecoder().decode(input);
            System.out.println(new String(decode));
            String s = Base64.getEncoder().encodeToString(decode);
            System.out.println(s);
            return input.equals(Base64.getEncoder().encodeToString(Base64.getDecoder().decode(input)));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return false;
        }
    }
}
